---@class DataManager:AbstractTemplateManager
local DataManager = ClientFight.CreateClass("DataManager",ClientFight.AbstractTemplateManager)
local this = DataManager
---@type table<number,T_heroBean>
this.t_heroBeanMap = {}
---@type table<number, T_skillBean>
this.t_skillBeanMap = {}
---@type table<number, T_buffBean>
this.t_buffBeanMap = {}
---@type table<number, HerospecialBean>
this.herospecialBeanMap = {}
---@type table<number, HerospecialBean>
this.bossHeroIdMap = {}
---@type table<number, ProfessionAdditionTemplate>
this.t_professionAdditionBeanMap = {}
---@type table<number, HeroCombinationTemplate>
this.heroCombinationTemplateMap = {}
---@type table<number, HeroCombinationShowTemplate> 
this.heroCombinationShowTemplateMap = {}
---主技能配置表 key1:技能id  key2:等级
---@type table<number, table<number, DebrisMajorSkillTemplate>>
this.majorSkillMapByLv = {}
---主技能配置表 key1:技能id  key2:等级
---@type table<number, table<number, DebrisMinorSkillTemplate>>
this.debrisMinorSkillTemplate = {}
---英雄技能等级解锁表 <英雄id,<技能类型，List<bean>>>
---@type table<number, table<number, T_skillunlockerBean[]>>
this.skillUnlockerMap = {}
---专武技能
---@type table<number, ArmskillBean>
this.armskillUnlockMap = {}

function DataManager.getModuleName()
    return "配置表"
end
function DataManager.init()
    this.loadData()
end
function DataManager.loadData()
    local cfg = dataTable.getDataTable("t_guildpara")
    ClientFight.GuildParaConstant.loadCfg(cfg)

    cfg = dataTable.getDataTable("t_global")
    this.initGlobalCont(cfg)
    ClientFight.Global.loadCfg(cfg)

    this.t_buffBeanMap = this.loadAll("t_buff",T_buffBean,"f_BuffId")
    this.t_skillBeanMap = this.loadAll("t_skill",T_skillBean,"f_SkillID")
    this.skillShowMap = G.dataTable["t_skillshow"];
    this.t_heroBeanMap = this.loadAll("t_hero",T_heroBean,"f_HeroID")
    this.herospecialBeanMap = this.loadAll("t_herospecial",HerospecialBean,"id")
    this.heroCombinationTemplateMap = this.loadAll("t_herocombination",HeroCombinationTemplate,"id")
    this.heroCombinationShowTemplateMap = this.loadAll("t_herocombinationshow",HeroCombinationShowTemplate,"heroId")

    ---@type T_skillunlockerBean[]
    local unLockerMap = this.loadAll("t_skillunlocker",T_skillunlockerBean,"skillId")
    for k,bean in pairs(unLockerMap) do
        if not this.skillUnlockerMap[bean.heroId] then
            this.skillUnlockerMap[bean.heroId] = {}
        end
        local heroSkillMap = this.skillUnlockerMap[bean.heroId]
        if not heroSkillMap[bean.skillType] then
            heroSkillMap[bean.skillType] = {}
        end
        table.insert(heroSkillMap[bean.skillType],bean)
    end
    for k,v in pairs(this.skillUnlockerMap) do
        for k2,v2 in pairs(v) do
            table.sort(v2,function(a,b)
                local i = b.f_SkillRelease - a.f_SkillRelease
                if i ~= 0 then
                    return b.f_SkillRelease < a.f_SkillRelease
                end
                return b.skillUnlocker < a.skillUnlocker
            end)
        end
    end
    this.t_professionAdditionBeanMap = this.loadAll("t_professionaddition",ProfessionAdditionTemplate,"professionId")

    this.afterLoadBuff()
    this.afterLoadSkill()

    ---@type DebrisMajorSkillTemplate[]
    local allSkillTemplate = this.loadAll("t_debrismajorskill",DebrisMajorSkillTemplate,"id")
    for k,v in pairs(allSkillTemplate) do
        if not this.majorSkillMapByLv[v.skillId] then
            this.majorSkillMapByLv[v.skillId] = {}
        end
        this.majorSkillMapByLv[v.skillId][v.lv] = v
    end

    for k,v in pairs(this.herospecialBeanMap) do
        if v.f_Type == 1 then
            this.bossHeroIdMap[v.f_HeroID] = v
        end
    end
    this.armskillUnlockMap = this.loadAll("t_armskill",ArmskillBean,"f_ArmSkill")
    this.debrisMinorSkillTemplate = this.loadAll("t_debrisminorskill",DebrisMinorSkillTemplate,"f_SkillId");
end
function DataManager.loadAll(cfgName,bean,id)
    local map = {}
        if bean == nil then
            local a = 1;
        end
    local cfg = dataTable.getDataTable(cfgName)
    if bean == nil then
        local a = 1;
    end
    for k,v in pairs(cfg) do
        local b = bean.New()
        b:loadCfg(v)
        map[b[id]] = b
    end
    return map
end
function DataManager.afterLoadSkill()
    for k,v in pairs(this.t_skillBeanMap) do
        v:afterLoadSkill()
    end
end
function DataManager.afterLoadBuff()
    for k,v in pairs(this.t_buffBeanMap) do
        v:afterLoadBuff()
    end
end
---@return T_skillunlockerBean
function DataManager.getSkillUnlockBean(heroId,lv,start,type)
    local heroSkillMap = this.skillUnlockerMap[tonumber(heroId)]
    if heroSkillMap == nil then return nil end
    local list = heroSkillMap[type]
    if list == nil then return nil end
    for k,bean in pairs(list) do
        local continue = false
        if lv < bean.skillUnlocker then
            continue = true
        end
        if start < bean.f_SkillRelease then
            continue = true
        end
        if not continue then
            return bean --加载配置表时,已经按倒叙排过了, 所以找到第一个满足条件的就可以返回了
        end
    end
    return nil
end
---@return ProfessionAdditionTemplate
function DataManager.getProfessionAdditionTemplate(professionId)
    local template = this.t_professionAdditionBeanMap[professionId]
    return template
end

function DataManager.initGlobalCont(globalTemplateMap)
    ClientFight.Global.FIGHT_SITES[1] = decodeJsonStrToTabel(globalTemplateMap["1"].f_string_value)
    ClientFight.Global.FIGHT_SITES[2] = decodeJsonStrToTabel(globalTemplateMap["4"].f_string_value);
    ClientFight.Global.MIN_ATTACK_ROUND =  tonumber(globalTemplateMap["2"].f_int_value);
    ClientFight.Global.FIGHT_INNER_ROUND = decodeJsonStrToTabel(globalTemplateMap["54"].f_string_value);
    ClientFight.Global.FIGHT_OUTER_ROUND = decodeJsonStrToTabel(globalTemplateMap["55"].f_string_value);
    ClientFight.Global.FIGHT_INNER_ROUND0 = decodeJsonStrToTabel(globalTemplateMap["62"].f_string_value);

    --/** 基础命中 */
    ClientFight.Global.FIGHT_HITRATE_BASE = tonumber(globalTemplateMap["47"].f_int_value);
    --/** 灵敏效果系数 */
    ClientFight.Global.FIGHT_SPEED_COEFFICIENT = tonumber(globalTemplateMap["49"].f_int_value);
    --/** 伤害下限系数 */
    ClientFight.Global.DAMAGE_MIN = tonumber(globalTemplateMap["98"].f_int_value);
    --/** 死亡回复能量 */
    ClientFight.Global.DIE_RECOVER_ENERGY = tonumber(globalTemplateMap["97"].f_int_value);
    --/** 受伤回复能量 */
    ClientFight.Global.HURT_RECOVER_ENERGY = tonumber(globalTemplateMap["96"].f_int_value);

    ClientFight.Global.ALTERNATE_POS_1 = decodeJsonStrToTabel(globalTemplateMap["183"].f_string_value);
    ClientFight.Global.ALTERNATE_POS_2 = decodeJsonStrToTabel(globalTemplateMap["184"].f_string_value);

    ClientFight.Global.HERO_CLASS_OPPRESS = decodeJsonStrToTabel(globalTemplateMap["187"].f_string_value);

    ClientFight.Global.FIGHT_SITES_UNION[1] = ClientFight.GuildParaConstant.FIGHT_SITES_SELF;
    ClientFight.Global.FIGHT_SITES_UNION[2] = ClientFight.Global.FIGHT_SITES[2];

    ClientFight.Global.MAX_FIGHT_SECONDS = tonumber(globalTemplateMap["135"].f_int_value);
    ClientFight.Global.MAX_FIGHT_ROUND = tonumber(globalTemplateMap["387"].f_int_value);
    --//        ClientFight.Global.FIGHT_SITES_FOR7[0] = decodeJsonStrToTabel(globalTemplateMap["407"]);
    --//        ClientFight.Global.FIGHT_SITES_FOR7[1] = decodeJsonStrToTabel(globalTemplateMap["408"]);
    ClientFight.Global.FIGHT_SITES_FOR7[1] = ClientFight.Global.FIGHT_SITES[1];
    ClientFight.Global.FIGHT_SITES_FOR7[2] = ClientFight.Global.FIGHT_SITES[2];
    ClientFight.Global.FIGHT_REPORT_VERSION = tonumber(globalTemplateMap["613"].f_int_value);
end
---@return DebrisMajorSkillTemplate
function DataManager.getMajorSkillTemplate(skillId,lv)
    if not this.majorSkillMapByLv[skillId] then
        return nil
    end
    return this.majorSkillMapByLv[skillId][lv]
end
---@return HerospecialBean
function DataManager.getByBossId(bossHeroId)
    return this.bossHeroIdMap[bossHeroId]
end
--[[
* 获取主技能配置表
* @param skillId  奇异碎片技能id
*/]]
function DataManager.getDebrisMinorSkillTemplate(skillId)
return this.debrisMinorSkillTemplate[skillId];
end
DataManager.init();